/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui;

import java.util.Iterator;

import net.sourceforge.perforce.ui.action.MarkResolvedAction;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.DiffTreeViewer;
import org.eclipse.compare.structuremergeviewer.IDiffContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Composite;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class P4ResolveEditorInput extends PerforceCompareEditorInput {
  // -------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for P4ResolveEditorInput.
   * 
   * @param left element displayed on the left.
   * @param right element displayed on the right.
   * @param ancestor element displayed in ancestor pane.
   */
  public P4ResolveEditorInput(ITypedElement left, ITypedElement right, ITypedElement ancestor) {
    super(left, right, ancestor);
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public Viewer createDiffViewer(Composite parent) {
    DiffTreeViewer v = new InternalDiffTreeViewer(parent, getCompareConfiguration());
    setDiffViewer(v);

    return v;
  }

  // ----------------------------------------------------------------------
  // Private Nested Classes

  /** Internal customization of the DiffTreeViewer. */
  private static class InternalDiffTreeViewer extends DiffTreeViewer {
    // -----------------------------------------------------------------------
    // Private Variables

    /** Stores the real mark resolved action. */
    private MarkResolvedAction action = new MarkResolvedAction();
    /** Stores the mark resolved action. */
    private Action markResolvedAction;

    // ---------------------------------------------------------------------
    // Public Constructors

    /**
     * Constructor for InternalDiffTreeViewer.
     * 
     * @param parent the parent composite.
     * @param configuration the configuration.
     */
    public InternalDiffTreeViewer(Composite parent, CompareConfiguration configuration) {
      super(parent, configuration);

      markResolvedAction = new Action("Mark as Resolved") {
        @SuppressWarnings("unchecked")
        // IDiffContainer
        @Override
        public void run() {
          action.run(this);

          IStructuredSelection selection = (IStructuredSelection) getSelection();
          for (Iterator<IDiffContainer> it = selection.iterator(); it.hasNext();) {
            IDiffContainer container = it.next();
            IDiffContainer parent = container.getParent();
            if (parent != null) {
              parent.removeToRoot(container);
            }
          }

          refresh();
        }
      };
    }

    //--------------------------------------------------------------------------
    // Public Methods

    @Override
    protected void fillContextMenu(IMenuManager manager) {
      IStructuredSelection selection = (IStructuredSelection) getSelection();
      for (Iterator<?> it = selection.iterator(); it.hasNext();) {
        Object o = it.next();
        if (o instanceof IAdaptable) {
          IAdaptable adaptable = (IAdaptable) o;
          if (adaptable.getAdapter(IResource.class) != null) {
            action.selectionChanged(markResolvedAction, getSelection());
            manager.add(markResolvedAction);
          }
        }
      }

      super.fillContextMenu(manager);
    }
  }
}
